package com.exrobot.module.rbt.service.robotfiles;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.exrobot.module.rbt.controller.admin.robotfiles.vo.*;
import com.exrobot.module.rbt.dal.dataobject.robotfiles.RobotFilesDO;
import com.exrobot.framework.common.pojo.PageResult;
import com.exrobot.framework.common.pojo.PageParam;
import com.exrobot.framework.common.util.object.BeanUtils;

import com.exrobot.module.rbt.dal.mysql.robotfiles.RobotFilesMapper;

import static com.exrobot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.exrobot.framework.common.util.collection.CollectionUtils.convertList;
import static com.exrobot.framework.common.util.collection.CollectionUtils.diffList;
import static com.exrobot.module.rbt.enums.ErrorCodeConstants.*;

/**
 * 机器人文件 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class RobotFilesServiceImpl implements RobotFilesService {

    @Resource
    private RobotFilesMapper robotFilesMapper;

    @Override
    public Long createRobotFiles(RobotFilesSaveReqVO createReqVO) {
        // 插入
        RobotFilesDO robotFiles = BeanUtils.toBean(createReqVO, RobotFilesDO.class);
        robotFilesMapper.insert(robotFiles);

        // 返回
        return robotFiles.getId();
    }

    @Override
    public void updateRobotFiles(RobotFilesSaveReqVO updateReqVO) {
        // 校验存在
        validateRobotFilesExists(updateReqVO.getId());
        // 更新
        RobotFilesDO updateObj = BeanUtils.toBean(updateReqVO, RobotFilesDO.class);
        robotFilesMapper.updateById(updateObj);
    }

    @Override
    public void deleteRobotFiles(Long id) {
        // 校验存在
        validateRobotFilesExists(id);
        // 删除
        robotFilesMapper.deleteById(id);
    }

    @Override
        public void deleteRobotFilesListByIds(List<Long> ids) {
        // 删除
        robotFilesMapper.deleteByIds(ids);
        }


    private void validateRobotFilesExists(Long id) {
        if (robotFilesMapper.selectById(id) == null) {
            throw exception(RBT_DATA_NOT_EXISTS);
        }
    }

    @Override
    public RobotFilesDO getRobotFiles(Long id) {
        return robotFilesMapper.selectById(id);
    }

    @Override
    public PageResult<RobotFilesDO> getRobotFilesPage(RobotFilesPageReqVO pageReqVO) {
        return robotFilesMapper.selectPage(pageReqVO);
    }

}